# 导入必要的库
import pandas as pd
import pymongo
from pandas import DataFrame

client = pymongo.MongoClient('localhost')
db = client['TV_info']
# 提取出品时间 2023年及其以前的电视剧
data_get = list(db['All_TV'].find({'出品时间': {'$lte': 2023}}))
data = DataFrame(data_get, columns=['电视剧', 'rate', '主演', '导演', '短评数', '类型', '出品地', '集数', '出品时间',
                                    '电视剧标签'])
# 将出品时间转换为数值型
dates = []
for i in range(len(data['出品时间'])):
    date = data['出品时间'].values[i]
    # 修改：使用pd.to_numeric()函数，将出品时间转换为数值型，并用errors='coerce'参数，将不能转换的值替换为NaN
    date = pd.to_numeric(date, errors='coerce')
    dates.append(date)
    data['出品时间'] = [dates] * len(data)
    # 修改：使用fillna(0)函数，将NaN值替换为0，然后再转换为整数
    data['出品时间'] = data['出品时间'].fillna(0).astype(int)
    # 将评分转换为数值型
    rates = []
    for i in range(len(data['rate'])):
        rate = data['rate'].values[i]
        if rate:
            rate = float(rate)
            rates.append(rate)
        else:
            rate = None
            rates.append(rate)
    data['rate'] = rates
    data = data.sort_values(by='出品时间', ascending=False)
    data.index = range(0, len(data))
    data = data[data['rate'].notnull()]
    data.index = range(0, len(data))
    # 去重
    data_new = data.iloc[[0], :]
    same = data_new
    for i in range(1, len(data)):
        info = data.iloc[i, :]
        if list(same) != list(info.values):
            same = info
            data_new = pd.concat([data_new, info], axis=1)
            pass
        else:
            same = info
    data = data_new.T


# 缺损处理
def Dropnull(ss, data):
    for i in range(0, len(data)):
        sss = data.ix[i, ss]
        if sss:
            pass
        else:
            data = data.drop([i])
    data.index = range(0, len(data))
    return data


# 删除导演为空的数据
data = Dropnull('导演', data)
# 删除主演为空的数据
data = Dropnull('主演', data)
# 删除短评数为空的数据
data = Dropnull('短评数', data)
# 删除集数为空的数据
data = Dropnull('集数', data)
# 删除类型为空的数据
data = Dropnull('类型', data)
# 删除出品地为空的数据
data = Dropnull('出品地', data)
# 删除出品时间为空的数据
data = Dropnull('出品时间', data)

# 方差其次性检验

from scipy import stats
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm

rate = data.iloc[:, ['出品地', 'rate']]
df_c = rate[rate['出品地'] == '国产电视剧']['rate']
df_m = rate[rate['出品地'] == '英美剧']['rate']
df_r = rate[rate['出品地'] == '日韩剧']['rate']
df = [df_c, df_m, df_r]
w, p = stats.levene(*df)
if p < 0.05:
    print('警告：levene test 检验出方差其次性假设不成立 %r' % p)
else:
    print('levene test 检验出方差其次性假设成立 %r' % p)

# 方差分析
rate = data.ix[:, ['出品地', 'rate']]
model = ols('rate~出品地', rate).fit()
result = anova_lm(model)
result
